package cn.zyjw.controller;

import cn.zyjw.domain.SysUser;
import cn.zyjw.interfaces.Add;
import cn.zyjw.model.R;
import cn.zyjw.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.groups.Default;
import java.util.List;

@RestController
@Api("员工管理")
@RequestMapping("/users")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @GetMapping
    @ApiOperation("员工列表分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "size", value = "每页展示条数"),
            @ApiImplicitParam(name = "mobile", value = "员工手机号"),
            @ApiImplicitParam(name = "fullname", value = "员工全称"),
    })
    @PreAuthorize("hasAuthority('sys_user_query')")
    public R<Page<SysUser>> userListByPage(@ApiIgnore Page<SysUser> page, String mobile, String fullname) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(mobile), "mobile", mobile)
                .like(StringUtils.isNotBlank(fullname), "fullname", fullname);
        Page<SysUser> userPage = sysUserService.pageList(page, queryWrapper);
        return R.ok(userPage);
    }

    @PostMapping
    @ApiOperation("新建员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sysUser", value = "sysUser json数据")
    })
    @PreAuthorize("hasAuthority('sys_user_create')")
    public R sava(@RequestBody @Validated({Add.class, Default.class}) SysUser sysUser) {
        boolean save = sysUserService.saveUser(sysUser);
        if (save) {
            return R.ok("新建员工成功");
        }
        return R.fail("新建员工失败");
    }


    @PatchMapping
    @ApiOperation("编辑员工")
    @ApiImplicitParams({
            @ApiImplicitParam
    })
    @PreAuthorize("hasAuthority('sys_user_update')")
    public R<String> updUser(@RequestBody SysUser sysUser) {
        boolean isOk = sysUserService.updUser(sysUser);
        if (isOk) {
            return R.ok("编辑员工成功");
        }
        return R.fail("编辑员工失败");
    }

    @PostMapping("/delete")
    @ApiOperation("批量删除员工")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "删除用户id数组")
    })
    @PreAuthorize("hasAuthority('sys_user_delete')")
    public R<String> del(@RequestBody Long[] ids) {
        boolean isOk = sysUserService.delUserList(ids);
        if (isOk) {
            return R.ok("删除员工成功");
        }
        return R.fail("删除员工失败");
    }

}
